package com.diandian.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * @description VerbalAssociation
 * @author TDW
 * @date 2018年8月8日
 *
 */
public class VerbalAssociation {
	
	private static final Logger logger = LoggerFactory.getLogger(VerbalAssociation.class);
	private static String filePath = "";
	private static String firstCutWordPath = "";
	public static ConcurrentHashMap<String, String> correctWords = new ConcurrentHashMap<String, String>();
	//private static Set<String> errWords = new HashSet<String>();
	public static Set<String> filterCutWords = new HashSet<String>();
	
	//易错此替换
//	private String[] errorVerbalAssociation={
//			"妙饭","鸡量"
//	};
	//词语替换
//	private String[] correctVerbalAssociation={
//			"炒饭","鸡蛋"
//	};
	
	public static void init(String verbalAssociationPath,String cutPath) {
		initVerbalAssociation(verbalAssociationPath);
		initFirstCutWord(cutPath);
	}
	
	public static void init(){
		
		String webInfoPath="";
		try {
			webInfoPath = new PathUtil().getWebRoot();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		filePath = webInfoPath+"/data/verbalAssociation.words";
		initVerbalAssociation(filePath);
		firstCutWordPath=webInfoPath+"/data/firstCut.words";
		initFirstCutWord(firstCutWordPath);
	}
	
	
	
	
	
	/**
	 * @description init
	 * @param verbalAssociationPath
	 * @author TDW
	 * @date 2018年8月8日
	 * @modify_date 
	 * @modify_author
	 * @modify_desc
	 */
	public static void initVerbalAssociation(String verbalAssociationPath) {
		filePath = verbalAssociationPath;
		logger.info("开始加载错词库...");
		BufferedReader read = null;
		try {
			read = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));
			String line = null;
			while(StringUtil.isNotBlank(line=read.readLine())){
				if(line.indexOf("#")==0)
					continue;
				String[] wordsStr = line.split("#");
				String errWord = wordsStr[0];
				String correctWord = wordsStr[1];
				correctWords.put(errWord, correctWord);
				//errWords.add(errWord);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(read != null){
				try {
					read.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		logger.info("错词库加载完毕!");
		logger.info("错词库：{}",correctWords.toString());
	}
	
	
	public static void initFirstCutWord(String cutPath) {
		firstCutWordPath = cutPath;
		logger.info("开始加载首滤词");
		BufferedReader read = null;
		try {
			read = new BufferedReader(new InputStreamReader(new FileInputStream(firstCutWordPath)));
			String line = null;
			while(StringUtil.isNotBlank(line=read.readLine())){
				if(line.indexOf("#")==0)
					continue;
				if(StringUtil.isBlank(line))
					continue;
				filterCutWords.add(line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(read != null){
				try {
					read.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		logger.info("开始加载首滤词加载完毕!");
		logger.info("开始加载首滤词库：{}",filterCutWords.toString());
	}
	
	
	
	
	
	
	
	/**
	 * @description replaceErrWord 替换错词
	 * @param content
	 * @return
	 * @author TDW
	 * @date 2018年8月8日
	 * @modify_date 
	 * @modify_author
	 * @modify_desc
	 */
	public static String replaceErrWord(String content){
		if(StringUtil.isBlank(content))
			return content;
		//not blank
		char[] chrs = content.toCharArray();
		String word = null;
		for(int i=0; i<chrs.length-1; i++){//两个汉字的词
			word = chrs[i]+""+chrs[i+1];
			String correctWord = correctWords.get(word);
			if(correctWord != null){
				logger.info("匹配错词【{}】替换成==>【{}】",word,correctWord);
				content = content.replaceAll(word,correctWord);
//				content = content.replaceAll(word,"<font color=red>"+correctWord+"</font>");
			}
		}
		return content;
	}
	
	/**
	 * @description cutFirstWord
	 * @param content
	 * @return
	 * @author TDW
	 * @date 2018年8月8日
	 * @modify_date 
	 * @modify_author
	 * @modify_desc
	 */
	public static String cutFirstWord(String content){
		if(StringUtil.isBlank(content))
			return content;
		//not blank
		char[] chrs = content.toCharArray();
		String tmpStr = null;
		for(int i=chrs.length-2; i>=0; i--){//最少保留一个字
			tmpStr = content.substring(0, i);
			if(filterCutWords.contains(tmpStr)){
				content = content.replace(tmpStr,"");
				break;
			}
		}
		return content;
	}


	/**
	 * @description addWords , 添加错词到错词库
	 * @param map
	 * @author TDW
	 * @date 2018年8月8日
	 * @modify_date 
	 * @modify_author
	 * @modify_desc
	 */
	public static void addWords(HashMap<String, String> map) {
//		BufferedWriter write = null;
		FileWriter write= null;
		try {
//			write = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath,true)));
			write = new FileWriter(filePath, true);
			for(Entry<String, String> entry : map.entrySet()){
				write.write("\n");
				write.write(entry.getKey()+"#"+entry.getValue());
			}
			write.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(write != null)
				try {
					write.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		correctWords.putAll(map);
	}
	
	
	
	/**
	 * @description addFirstCutWords 
	 * @param map
	 * @author TDW
	 * @date 2018年8月8日
	 * @modify_date 
	 * @modify_author
	 * @modify_desc
	 */
	public static void addFirstCutWords(HashSet< String> set) {
//		BufferedWriter write = null;
		FileWriter write= null;
		try {
//			write = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath,true)));
			write = new FileWriter(filePath, true);
			for(String entry : set){
				write.write("\n");
				write.write(entry);
			}
			write.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(write != null)
				try {
					write.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		filterCutWords.addAll(set);
	}
	

}
